package kr.hkit.androidcomplete03.ch27;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CopyTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch27_copytext);
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch27_copytest_copy_btn:
			copyText();
			break;

		case R.id.ch27_copytest_paste_btn:
			pasteText();
			break;
		}
	}

	private void pasteText() {
		ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		
		if(cm.hasPrimaryClip() == false){
			Toast.makeText(this, "Clipboard Empty", 0).show();
			return;
		}
		
		if(cm.getPrimaryClipDescription().hasMimeType
				(ClipDescription.MIMETYPE_TEXT_PLAIN) == false){
			Toast.makeText(this, "Clip is not text", 0).show();
			return;
		}
		
		ClipData clip = cm.getPrimaryClip();
		ClipData.Item item = clip.getItemAt(0);
		TextView pastetext = (TextView) findViewById(R.id.ch27_copytest_paste_tv);
		pastetext.setText(item.getText());
	}

	private void copyText() {
		EditText copyEdit = (EditText) findViewById(R.id.ch27_copytest_edit);
		String text = copyEdit.getText().toString();
		if(text.length() != 0){
			ClipData clip = ClipData.newPlainText("text", text);
			ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			cm.setPrimaryClip(clip);
			Toast.makeText(this, "Text Copied", 0).show();
		}
	}
	
	
}
